using System;
using ScenarioFramework.WorkflowEngine.DataModel;

namespace ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses
{	
	public class XMLScript : bOSdotNET.XML.Data.XMLObject
	{
		public const string MAIN_NODE				= "Script";

		private const string TYPE_ATTRIBUTE			= "Type";
		private const string DATATYPE_ATTRIBUTE		= "DataType";

		public const string NONE_TYPE				= "NONE";
		public const string SUBFLOW_SYNCH_TYPE		= "SUBFLOW_SYNCH";
		public const string SUBFLOW_ASYNC_TYPE		= "SUBFLOW_ASYNC";
		public const string SUBFLOW_SPECIAL_TYPE	= "SUBFLOW_SPECIAL";
		public const string API_SYNCH_TYPE			= "API_SYNCH";
		public const string API_ASYNC_TYPE			= "API_ASYNC";
		public const string EMAIL_SMTP_TYPE			= "EMAIL";
		public const string APPLICATION_API_TYPE	= "APPLICATION";
		public const string XML_XSLT_TYPE			= "XSLT";
		public const string EVENT_MAKER_TYPE		= "EVENT_MAKER";
		public const string EVENT_CONSUMER_TYPE		= "EVENT_CONSUMER";
		public const string EVAL_EXPRESSION_TYPE	= "EVAL_EXPRESSION";
		public const string LOOP_COUNTER_TYPE		= "LOOP_COUNTER";
		public const string LOOP_EXPRESSION_TYPE	= "LOOP_EXPRESSION";

		public const string NONE_ACTION = "[NONE]";

		private string sType;
		private string sDataType;
		private string sValue;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLScript()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLScript(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string DataType 
		{
			get { return sDataType; }
			set { sDataType = value; }
		}

		public string Value 
		{
			get { return sValue; }
			set { sValue = value; }
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			sType = string.Empty;
			sDataType = string.Empty;
			sValue = string.Empty;
		}

		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			
			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLScript.TYPE_ATTRIBUTE:
						sType = node.InnerText;
						break;
					case XMLScript.DATATYPE_ATTRIBUTE:
						sDataType = node.InnerText;
						break;
				}
			}

			sValue = node2Parse.InnerXml;
		}
		
		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLScript.MAIN_NODE, sValue);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLScript.TYPE_ATTRIBUTE, sType) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLScript.DATATYPE_ATTRIBUTE, sDataType) );
			
			return xmlNode;
		}

		public void Set2None()
		{
			sType=XMLScript.NONE_TYPE;
			sDataType=XMLScript.NONE_TYPE;
			sValue="[NONE]";
		}
	}
}
